package com.sc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.model.SoftmgrProject;
import com.acc.softmgr.model.SoftmgrProjectExample;
import com.acc.softmgr.model.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sc.softmgr.business.service.ISoftmgrProjectService;
import com.sc.softmgr.common.constant.DeleteStatus;
import com.sc.softmgr.common.constant.enums.RestStatus;
import com.sc.softmgr.common.utils.date.DateBase;
import com.sc.softmgr.common.web.DataGridObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("softProject")
public class SoftmgrProjectController {

	@Autowired
	private ISoftmgrProjectService softmgrProjectService;

	@RequestMapping(value = "toSoftProject", method = RequestMethod.GET)
	public String toSoftProject() {
		return "softProject/softProject";
	}

	/**
	 * 查询列表页
	 * 
	 * @param project_name
	 * @param project_english
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSoftProject", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SoftmgrProject> searchSoftProject(SoftmgrProject softmgrProject, @RequestParam Integer page,
			@RequestParam Integer rows) throws Exception {
		DataGridObject<SoftmgrProject> responseObject = new DataGridObject<SoftmgrProject>();
		SoftmgrProjectExample example = new SoftmgrProjectExample();
		if (!StringUtils.isEmpty(softmgrProject.getProject_name())) {
			example.getCriteria().andProject_nameLike("%" + softmgrProject.getProject_name() + "%");
		}
		if (page == null || page == 0) {
			page = 1;
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SoftmgrProject> softmgrProjects = softmgrProjectService.selectByExample(example);
		for (SoftmgrProject sp : softmgrProjects) {
			sp.setCreateTimestring(DateBase.formatDate(sp.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(softmgrProjects);
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addSoftProject", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject addSoftProject(SoftmgrProject softmgrProject, HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		if (StringUtils.isEmpty(softmgrProject.getProject_name())
				|| StringUtils.isEmpty(softmgrProject.getProject_english())) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		} else {
			// ----------判断唯一性------------
			SoftmgrProjectExample example = new SoftmgrProjectExample();
			example.getCriteria().andProject_nameEqualTo(softmgrProject.getProject_name());
			example.getCriteria().andProject_englishEqualTo(softmgrProject.getProject_english());
			example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
			List<SoftmgrProject> softmgrProjects = softmgrProjectService.selectByExample(example);
			if (softmgrProjects != null && softmgrProjects.size() > 0) {
				json.put("status", RestStatus.NOT_UNIQUE.code);
				json.put("message", RestStatus.NOT_UNIQUE.message);
				return json;
			} else {
				SoftmgrProject sp = new SoftmgrProject();
				sp.setCreatetime(new Date());
				sp.setCreateby(currentUser.getUsername());
				sp.setProject_english(softmgrProject.getProject_english());
				sp.setProject_name(softmgrProject.getProject_name());
				sp.setDeleted(DeleteStatus.FALSE);
				softmgrProjectService.insert(sp);
				json.put("status", RestStatus.SUCCESS.code);
				json.put("message", RestStatus.SUCCESS.message);
				return json;
			}

		}

	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSoftProject", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject editSoftProject(SoftmgrProject softmgrProject, HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		if (StringUtils.isEmpty(softmgrProject.getProject_name())
				|| StringUtils.isEmpty(softmgrProject.getProject_english()) || softmgrProject.getId() == null
				|| softmgrProject.getId() == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SoftmgrProject sp = softmgrProjectService.selectByPrimaryKey(softmgrProject.getId());
		boolean exitSoftProject = exitSoftmgrProject(softmgrProject.getProject_name(),
				softmgrProject.getProject_english());
		if (exitSoftProject) {
			json.put("status", RestStatus.NOT_UNIQUE.code);
			json.put("message", RestStatus.NOT_UNIQUE.message);
			return json;
		}
		sp.setProject_name(softmgrProject.getProject_name());
		sp.setProject_english(softmgrProject.getProject_english());
		sp.setUpdateby(currentUser.getUsername());
		sp.setCreatetime(new Date());
		softmgrProjectService.updateByPrimaryKey(sp);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSoftProject", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchSoftProject(@PathVariable Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SoftmgrProject softmgrProject = softmgrProjectService.selectByPrimaryKey(id);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		json.put("data", softmgrProject);
		return json;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSoftProject", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject removeSoftProject(@RequestParam("id") Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SoftmgrProject softmgrProject = softmgrProjectService.selectByPrimaryKey(id);
		softmgrProject.setDeleted(DeleteStatus.TRUE);
		softmgrProjectService.updateByPrimaryKey(softmgrProject);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}

	/**
	 * 获取所有project项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getSoftProjects", method = RequestMethod.GET)
	@ResponseBody
	public String getSoftProjects() throws Exception {

		SoftmgrProjectExample example = new SoftmgrProjectExample();
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);

		List<SoftmgrProject> list = softmgrProjectService.selectByExample(example);
		JSONArray jsonArray = new JSONArray();
		for (SoftmgrProject project : list) {
			JSONObject json = new JSONObject();
			json.put("id", project.getId());
			json.put("text", project.getProject_name());
			jsonArray.add(json);
		}

		return jsonArray.toJSONString();
	}

	private boolean exitSoftmgrProject(String project_name, String project_english) {
		SoftmgrProjectExample example = new SoftmgrProjectExample();
		example.getCriteria().andProject_nameEqualTo(project_name);
		example.getCriteria().andProject_englishEqualTo(project_english);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrProject> softmgrProjects = softmgrProjectService.selectByExample(example);
		if (softmgrProjects != null && softmgrProjects.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

}
